//
//  RCMerchantDetailOtherShopViewController.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-9-11.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMerchantDetailOtherShopViewController.h"
#import "RCMerchantsListDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "UIColor-Expanded.h"
#import "RCMerchantListCell.h"
#import "NSArray+EX.h"
#import "GloableEngineService.h"
#import "RCMerchantDetailVC.h"
#import "AppDelegate.h"


#define kTableViewFrame            CGRectMake(0,0,320,UIScreenHeight-64)

@interface RCMerchantDetailOtherShopViewController ()

@property(nonatomic,retain) UITableView *shopTabelView;
@property(nonatomic,assign) int64_t requestID;
@property(nonatomic,retain) MBProgressHUDEx *progressHub;
@property(nonatomic,retain) RCMerchantsListDataModel *shopDataModel;
@property(nonatomic,retain) NSString *shopID;
@property(nonatomic,retain) NSString *lat;
@property(nonatomic,retain) NSString *lon;

@end

@implementation RCMerchantDetailOtherShopViewController

@synthesize shopTabelView = _shopTabelView;
@synthesize requestID = _requestID;
@synthesize progressHub = _progressHub;
@synthesize shopID = _shopID;
@synthesize shopDataModel = _shopDataModel;
@synthesize lat = _lat;
@synthesize lon = _lon;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithShopID:(NSString *)shopid withLat:(NSString *)lat withLon:(NSString *)lon{
    self = [super init];
    if (self) {
        self.shopID = shopid;
        self.lat = lat;
        self.lon = lon;
    }
    return self;
}

-(void)dealloc{
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
    _requestID = kInvalidRequestId;
    self.shopTabelView.delegate = nil;
    self.shopTabelView.dataSource = nil;
    self.shopTabelView = nil;
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.shopID = nil;
    self.shopDataModel = nil;
    self.lat = nil;
    self.lon = nil;
    [super dealloc];
}

-(void)loadView{
    [super loadView];
    self.titleLabelText = @"商户列表";
    self.navigationItem.titleView = [self createTitleView];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    if (!_shopTabelView) {
        _shopTabelView = [[UITableView alloc]initWithFrame:kTableViewFrame style:UITableViewStylePlain];
        _shopTabelView.backgroundColor = [UIColor clearColor];
        _shopTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _shopTabelView.delegate = self;
        _shopTabelView.dataSource = self;
    }
    [self.viewContainer addSubview:_shopTabelView];
    
    [self requestData];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
    _requestID = kInvalidRequestId;
}

-(void)requestData{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _requestID = [[IManager sharedInstance]requestMerchantDetailOtherShop:self.shopID withLat:self.lat withLon:self.lon withDelegate:self];
}

-(void)updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
        self.shopDataModel = (RCMerchantsListDataModel *)dataModel;
        [_shopTabelView reloadData];
    }
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        [_progressHub removeFromSuperview];
        [_progressHub release];
        _progressHub = nil;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (nil == self.shopDataModel || nil == self.shopDataModel.listItems || [self.shopDataModel.listItems count] == 0) {
        return 0;
    }
    return [self.shopDataModel.listItems count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (nil == self.shopDataModel || nil == self.shopDataModel.listItems || [self.shopDataModel.listItems count] == 0) {
        return 0;
    }
    
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"RCMerchantDetailOtherShopCell";
    RCMerchantListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[RCMerchantListCell alloc] init] autorelease];
    }
    RCMerchant *merchant = [self.shopDataModel.listItems safeObjectAtIndex:[indexPath row]];
    if (nil == merchant) {
        return nil;
    }
    [cell bindData:merchant];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if ([GloableEngineService sharedInstance].netWorkStatus == kNotReachable) {
        [RCAppDelegate alertWithNetworkError];
        return;
    }
    RCMerchant *merchant = [self.shopDataModel.listItems safeObjectAtIndex:[indexPath row]];
    if (nil == merchant) {
        return;
    }
    RCMerchantDetailVC *detail = [[RCMerchantDetailVC alloc] initWithMerchantID:merchant.merchantID];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
